﻿/*********************************************************************
 *
 *                             NOTICE
 *               COPYRIGHT (c) 2012 Panoscopix Software, Inc.
 *                UNPUBLISHED - ALL RIGHTS RESERVED
 * 
 *      This program is confidential, proprietary, and a trade
 *   secret of Panoscopix Software, Inc. The receipt or possession of
 *     this program does not convey any rights to reproduce or
 *      disclose its contents, or to manufacture, use, or sell
 *       anything that it may describe, in whole or in part, 
 *   without the specific written consent of Panoscopix Software, Inc.
 *
 * A simple in-memory implementation of a File.
 * 
 *******************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using FileSystem;
using FileSystem.Utility;

namespace FileSystem.Impl
{
    class FSFile : IFSFile
    {
	    private NodeMetaData  metaData;
        private IFSDirectory   parent;
	    private StringBuilder contents;
	
	    public FSFile(INodeMetaDataVO metaData, String contentsData) {
            this.metaData = metaData.copy();
            parent = null;
		    contents = new StringBuilder();
		    contents.Append(contentsData);
		    this.metaData.size = contents.Length;
	    }

        public IFSDirectory getParent() {
            return parent;
        }

        public void setParent(IFSDirectory parent) {
            this.parent = parent;
        }

        public String getID() {
		    return metaData.id;
	    }
	
	    public String getName() {
		    return metaData.name;
	    }

        public String getFQName() {
            if (null == parent) {
                return "[ORPHAN]/" + getName();
            }
            return parent.getFQName() + getName();
        }

	    public void rename(String newName) {
		    metaData.name = newName;
	    }
	
	    public int getSize() {
		    return metaData.size;
	    }
	
	    public String getCreationTimestamp() {
		    return metaData.getCreationTimestamp();
	    }
	
	    public String getContents() {
		    return contents.ToString();
	    }

        public void setContents(String text) {
            contents = new StringBuilder();
            contents.Append(text);
            metaData.size = contents.Length;
        }

	    public void appendContents(String text) {
		    if (!String.IsNullOrEmpty(text)) {
			    contents.Append(text);
		    }
		    contents.Append("\n");
		    metaData.size = contents.Length;
	    }
	
	    public String describe() {
            return "File " + getName() + "(" + getID() + ":" + getParentID() +
                "), created = " + getCreationTimestamp() + ", size = " + getSize();
	    }
	
	    public String describe(IPrintContext context) {
		    return context.getFormattedString(describe());
	    }
	
	    public int numNodes() {
		    return 0;
	    }
	
	    public IFSNode getNode(int i) {
		    return null;
	    }

	    public bool addNode(IFSNode node) {
		    return false;
	    }

	    public IFSNode removeNode(String name) {
		    return null;
	    }

        private String getParentID() {
            if (null == parent) return "null";
            return parent.getID();
        }

        #region IFSNode Members


        public void Accept(INodeVisitor visitor)
        {
            visitor.VisitFile(this);
        }

        #endregion
    }
}
